package com.xgpro.pc.studycentre.controller;

import com.xgpro.common.controller.BaseController;
import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.common.results.ResponseResult;
import com.xgpro.domain.studycentre.entity.Course;
import com.xgpro.domain.studycentre.vo.*;
import com.xgpro.pc.studycentre.service.CoursePCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

/**
 * @author tx
 * @version 1.0
 * @date 2019/10/14 21:17
 */
@RestController
@RequestMapping("/coursePC")
@CrossOrigin(allowCredentials="true")
public class CoursePCController  extends BaseController {
    @Autowired
    private CoursePCService coursePCService;

    /**
     * 查询开课课程信息待审批
     * @param coursePCFront
     * @return
     */
    @RequestMapping("/qryCoursePCResult")
    public ResponseResult qryCoursePCResult(HttpSession httpSession,@RequestBody CoursePCFront coursePCFront) {
        try{
            Integer id=(Integer)httpSession.getAttribute("id");
            List<Integer> roleIds=(List<Integer>)httpSession.getAttribute("roleIds");
            if (roleIds == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(coursePCService.qryCoursePCResult(coursePCFront,roleIds,id));
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 查询授课完毕课程
     * @param httpSession
     * @param coursePCFront
     * @return
     */
    @RequestMapping("/qryOverCoursePCResult")
    public ResponseResult qryOverCoursePCResult(HttpSession httpSession,@RequestBody CoursePCFront coursePCFront){
        try{
            List<Integer> roleIds=(List<Integer>)httpSession.getAttribute("roleIds");
            if (roleIds == null) {
                throw new BusinessRuntimeException(ResultCodeEnum.NO_SESSION.getCode(), ResultCodeEnum.NO_SESSION.getMessage());
            }
            return ResponseResult.ok(coursePCService.qryOverCoursePCResult(coursePCFront,roleIds));
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 审批授课完成课程
     * @param courseId
     * @param teaStatus
     * @return
     */
    @RequestMapping("/approvalOverCourse")
    public ResponseResult approvalOverCourse(Integer courseId,Integer teaStatus){
        try{
            Integer result=coursePCService.approvalOverCourse(courseId,teaStatus);
            if(result.equals(0)){
                throw new BusinessRuntimeException(ResultCodeEnum.ADD_FAILE.getCode(),ResultCodeEnum.ADD_FAILE.getMessage());
            }
            return ResponseResult.ok(result);
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 查询开课课程
     * @param courseOpenPCFront
     * @return
     */
    @RequestMapping("/qryCourseOpenResult")
    public ResponseResult qryCourseOpenResult(@RequestBody CourseOpenPCFront courseOpenPCFront){
        return ResponseResult.ok(coursePCService.qryCourseOpenResult(courseOpenPCFront));
    }

    /**
     * 根据课程id删除课程
     * @param id
     * @return
     */
    @RequestMapping("/deleteCourseOpenById")
    public ResponseResult deleteCourseOpenById(Integer id){
        //删除课程,是否需要删除t_user_course的记录
        return ResponseResult.ok(coursePCService.deleteCourseOpenById(id));
    }

    /**
     * 批量删除课程
     * @param ids
     * @return
     */
    @RequestMapping("/deleteCourseOpenByIds")
    public ResponseResult deleteCourseOpenByIds(@RequestBody List<Integer> ids){
        return ResponseResult.ok(coursePCService.deleteCourseOpenByIds(ids));
    }

    /**
     * 新增微课程
     * @param smallCourseFront
     * @return
     */
    @RequestMapping("/insertSmallCourse")
    public ResponseResult insertSmallCourse(SmallCourseFront smallCourseFront){
        try{
            return ResponseResult.ok(coursePCService.insertSmallCourse(smallCourseFront));
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 更新微课程
     * @param smallCourseFront
     * @return
     */
    @RequestMapping("/updateSmallCourse")
    public ResponseResult updateSmallCourse(SmallCourseFront smallCourseFront){
        try{
            return ResponseResult.ok(coursePCService.updateSmallCourse(smallCourseFront));
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 新增外部课程
     * @param courseOutFront
     * @return
     * @throws ParseException
     */
    @RequestMapping("/addOutCourse")
    public ResponseResult addOutCourse(CourseOutFront courseOutFront) throws ParseException {
           return ResponseResult.ok(coursePCService.addOutCourse(courseOutFront));
    }

    /**
     * 修改外部课程
     * @param courseOutFront
     * @return
     * @throws ParseException
     */
    @RequestMapping("/updateOutCourse")
    public ResponseResult updateOutCourse(
            CourseOutFront courseOutFront)throws ParseException{
           return ResponseResult.ok(coursePCService.updateOutCourse(courseOutFront));
    }

    /**
     * 查看微课程
     * @param id
     * @return
     */
    @RequestMapping("/qrySmallCourseById")
    public ResponseResult qrySmallCourseById(Integer id){
            return ResponseResult.ok(coursePCService.qrySmallCourseById(id));
    }

    /**
     * 查看课程详情
     * @param id
     * @return
     */
    @RequestMapping("/qryCourseById")
    public ResponseResult qryCourseById(Integer id){
            return  ResponseResult.ok(coursePCService.qryCourseById(id));
    }

    /**
     * 设置精品课程
     * @param id
     * @return
     */
    @RequestMapping("/setGoodCourse")
    public ResponseResult setGoodCourse(Integer id){
        return  ResponseResult.ok(coursePCService.setGoodCourse(id));
    }

    /**
     * 课程评估下载
     * @param id
     * @return
     */
    @RequestMapping("/downZipForCourse")
    public ResponseResult downZipForCourse(Integer id){
        try{
            return ResponseResult.ok(coursePCService.downZipForCourse(id));
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 批量导入课程
     * @param multipartFile
     * @return
     */
    @RequestMapping("/importCourse")
    public ResponseResult importCourse(MultipartFile multipartFile){
        try{
            return ResponseResult.ok(coursePCService.importCourse(multipartFile));
        }catch (BusinessRuntimeException e){
            return ResponseResult.failure(e.getErrorCode(),e.getMessage());
        }
    }
}
